package com.kuangstudy.service.creatorid;

import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Service;

/**
 * @author 飞哥
 * @Title: 学相伴出品
 * @Description: 我们有一个学习网站：https://www.kuangstudy.com
 * @date 2021/5/20 18:42
 */
@Service
@Log4j2
public class IdCreatorService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @return java.lang.Long
     * @Author 徐柯
     * @Description 生成全局id
     * @Date 18:50 2021/5/20
     * @Param []
     **/
    public Long incrmentid() {
        RedisAtomicLong entityIdCounter = new RedisAtomicLong("id:creator:product", redisTemplate.getConnectionFactory());//0
        // 计数器累加
        Long increment = entityIdCounter.incrementAndGet();
        // 或者
        //Long increment = redisTemplate.opsForValue().increment(key);
        return increment;
    }


    /**
     * @return java.lang.Long
     * @Author 徐柯
     * @Description 生成全局id
     * @Date 18:50 2021/5/20
     * @Param []
     **/
    public Long incrmentid(String key) {
        Long increment = redisTemplate.opsForValue().increment(key);
        return increment;
    }

}
